var server = require('../../utils/requestUtils.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inp: '',
    mp3url: '',
    txt: ["你有什么话想对我说？？？"],
    txtinput: '',
    scrollTop: 110
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.innerAudioContext = wx.createInnerAudioContext();
    this.innerAudioContext.onError((res) => {
      that.tip("播放录音失败！")
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  inp: function (e) {
    this.setData({
      inp: e.detail.value
    })

  },
  send: function () {
    wx.showLoading({
      title: '转换中...',
    })
    console.log("123")
    var that = this
    server.goGet('/voice/text2audio?content=' + that.data.inp, {

    }, function (re) {
      wx.hideLoading()
      that.setData({
        mp3url: re.url
      })
      console.log(re.url)
    })
  },
  play: function () {
    var that = this;
    var src = this.data.mp3url;
    if (src == '') {
      wx.showToast({
        title: '请先转换！',
        icon: 'none'
      })
      return;
    }
    this.innerAudioContext.src = this.data.mp3url;
    this.innerAudioContext.play()
  },
  out: function () {
    this.innerAudioContext.stop()
  },
  switch1Change: function (e) {
    if (e.detail.value == false) {
      this.innerAudioContext.loop = false
    }
    if (e.detail.value == true) {
      this.innerAudioContext.loop = true
    }
  },
  sd: function () {
    var that = this
    wx.request({
      url: 'https://openapi.tuling123.com/openapi/api/v2',
      method: 'post',
      header: {
        'Content-Type': 'application/json',
      },
      data: {
        "reqType": 0,
        "perception": {
          "inputText": {
            "text": that.data.txtinput
          }
        },
        "userInfo": {
          "apiKey": "9d0edf9551224b72a8f9e98be3244ada",
          "userId": "1123123"
        }
      },

      success: function (res) {
        console.log(res.data)
        that.setData({
          txtinput: '',
          txt: that.data.txt.concat(res.data.results[0].values.text),
          scrollTop: that.data.scrollTop + 10
        })
      },
      fail: function () {
        wx.showToast({
          title: '我累了，一会再来吧！',
          icon: 'none',
          duration: 5000
        })
        return
      }
    })


  },
  txt: function (e) {
    var that = this
    this.setData({
      txtinput: e.detail.value,
    })
  },
  scroll: function (e) {
    console.log(e)
  }
})